package com.qkl.xiatang.app.service.pc;

import com.alibaba.fastjson.JSON;
import com.qkl.xiatang.app.dto.wvp.*;
import com.qkl.xiatang.app.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PCService {

    private static final String PC_BASE_URL = "https://xiajiang.qkl88.cn/xiatang/";

    private static final String CHANNEL_REFRESH_URL = PC_BASE_URL + "wvpApi/channel/list";
    //通用前端控制命令
    private static final String CONTROL_FRONT_END_URL = PC_BASE_URL +  "wvpApi/channel/front_end_command";
    //云台控制
    private static final String CONTROL_CLOUD_URL = PC_BASE_URL +  "wvpApi/channel/control";
    //开始视频回放
    private static final String PLAY_BACK_START_URL = PC_BASE_URL +  "wvpApi/channel/playBackStart";
    //停止视频回放
    private static final String PLAY_BACK_STOP_URL = PC_BASE_URL +  "wvpApi/channel/playBackStop";
    //回放倍速播放
    private static final String PLAY_BACK_SPEED_URL = PC_BASE_URL +  "wvpApi/channel/playBackSpeed";
    //回放拖动播放
    private static final String PLAY_BACK_SEEK_URL = PC_BASE_URL +  "wvpApi/channel/playBackSeek";
    //回放恢复
    private static final String PLAY_BACK_RESUME_URL = PC_BASE_URL +  "wvpApi/channel/playBackResume";
    //回放暂停
    private static final String PLAY_BACK_PAUSE_URL = PC_BASE_URL +  "wvpApi/channel/playBackPause";
    //录像查询
    private static final String PLAY_BACK_RECORD_QUERY_URL = PC_BASE_URL +  "wvpApi/channel/recordQuery";


    public Result channelRefresh(String deviceId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("deviceId",deviceId);
        MD5Util.setSign(paramMap);
        HttpResponse<String> response = HttpUtils.getRequest(CHANNEL_REFRESH_URL,paramMap);
        return toResult(response);
    }

    /**
     * 录像查询
     * @param dto
     * @return
     */
    public Result playBack(PlayBackRecordQueryDTO dto){
        return this.playBack(PLAY_BACK_RECORD_QUERY_URL,dto);
    }

    /**
     * 开始视频回放
     * @param dto
     * @return
     */
    public Result playBack(PlayBackStartDTO dto){
        return this.playBack(PLAY_BACK_START_URL,dto);
    }
    /**
     * 停止视频回放
     * @param dto
     * @return
     */
    public Result playBack(PlayBackStopDTO dto){
        return this.playBack(PLAY_BACK_STOP_URL,dto);
    }

    /**
     * 回放倍速播放
     * @param dto
     * @return
     */
    public Result playBack(PlayBackSpeedDTO dto){
        return this.playBack(PLAY_BACK_SPEED_URL,dto);
    }

    /**
     * 回放拖动播放
     * @param dto
     * @return
     */
    public Result playBack(PlayBackSeekDTO dto){
        return this.playBack(PLAY_BACK_SEEK_URL,dto);
    }

    /**
     * 回放恢复
     * @param dto
     * @return
     */
    public Result playBack(PlayBackResumeDTO dto){
        return this.playBack(PLAY_BACK_RESUME_URL,dto);
    }
    /**
     * 回放暂停
     * @param dto
     * @return
     */
    public Result playBack(PlayBackPauseDTO dto){
        return this.playBack(PLAY_BACK_PAUSE_URL,dto);
    }

    private Result playBack(String url,IPlayBack playBack){
        Map<String,Object> params = playBack.toParams();
        MD5Util.setSign(params);
        HttpResponse<String> response = HttpUtils.getRequest(url,params);
        return toResult(response);
    }


    /**
     * 通用前端控制命令
     * @param dto
     * @return
     */
    public Result controlFrontEnd(ControlFrontEndDTO dto){
        Map<String,String> params = dto.toParams();
        MD5Util.setSign2(params);
        log.info("=====controlFrontEnd.params={}",params);
        HttpResponse<String> response = HttpUtils.postRequestForm(CONTROL_FRONT_END_URL,params);
        return toResult(response);
    }

    /**
     * 云台控制
     * @param dto
     * @return
     */
    public Result controlCloud(ControlCloudDTO dto){
        Map<String,String> params = dto.toParams();
        MD5Util.setSign2(params);
        log.info("=====controlCloud.params={}",params);
        HttpResponse<String> response = HttpUtils.postRequestForm(CONTROL_CLOUD_URL,params);
        return toResult(response);
    }

    private static Result toResult(HttpResponse<String> response){
        if(response.isSuccess()){
            return JSON.parseObject(response.getData(),Result.class);
        }else {
            return Result.error(ErrorCodeUtils.PC_SYSTEM_ERROR,response.getMsg());
        }
    }

}
